package tools

import (
	"SSO/config"
	"fmt"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
)

var db *gorm.DB

func initMysqlDB() {
	var err error
	c := config.GetConf().DB
	str := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8", c.Username, c.Password, c.Host, c.Port, c.Database)
	db, err = gorm.Open("mysql", str)
	if err != nil {
		fmt.Println(err.Error())
		panic(err)
	}
}

func GetMysqlDB() *gorm.DB {
	if db == nil {
		initMysqlDB()
	}
	return db
}

func CloseMysqlDB() {
	_ = db.Close()
}
